/*
 * @Author: wwssaabb
 * @Date: 2021-12-10 16:41:33
 * @LastEditTime: 2021-12-10 17:00:42
 * @FilePath: \handwritten-code\main\解析URL参数为对象.js
 */

//解析url的query字段

function getQuery(str) {
  let res = str.match(/(?<=\?).+/)
  return res && res[0].split('&').reduce((pre, cur) => {
    const [key, value] = cur.split('=')
    pre[key] = value !== '' ? value : undefined
    return pre
  }, {})
}

console.log(getQuery('https://c.runoob.com/front-end/854')) //null
console.log(getQuery('https://c.runoob.com/front-end/854/?optionGlobl=global&name=aabb&age=')) //{ optionGlobl: 'global', name: 'aabb', age: undefined }
console.log(getQuery('https://www.bilibili.com/video/BV1j54y157Er/?spm_id_from=333.788.recommend_more_video.0')) //{ spm_id_from: '333.788.recommend_more_video.0' }